package com.guomao.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;

//等价于创建了application.xml
@Configuration
@Import(MybatisConfig.class)
@PropertySource(value = "classpath:db.properties")
@ComponentScan("com.guomao.dao")
public class AppConfig {

    //读取加载的db.properties的jdbc.driver赋值给driverClassName
    @Value("${jdbc.driver}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${jdbc.username}")
    private String username;

    @Value("${jdbc.password}")
    private String password;

    @Bean
    public DruidDataSource getDataSource() {
        DruidDataSource ds = new DruidDataSource();
//        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
//        ds.setUsername("root");
//        ds.setPassword("666666");
//        ds.setUrl("jdbc:mysql://localhost:3306/jdbc_template");
        ds.setDriverClassName(driverClassName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setUrl(url);
        return ds;
    }


    @Bean
    public JdbcTemplate getJdbcTemplate(DruidDataSource ds) {
        return new JdbcTemplate(ds);
    }
}
